package com.xcf.controller.business.finance;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.service.business.finance.GoTicketAndIntoTicketService;
import com.xcf.service.business.finance.IntoTicketService;
import com.xcf.service.business.finance.InvoiceService;
import com.xcf.util.Jurisdiction;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;
import com.xcf.util.TranscodUntil;

/***
 * 发票管理
 * @author tanwei
 */
@Controller
@RequestMapping(value="/invoiceMgr")
public class InvoiceController extends BaseController {
	
	String menuUrl = "invoiceMgr/index"; //菜单地址(权限用)
    final String base = "/business/finance";
	private static final String PREFIX = "/business/finance/";
	@Autowired
	private InvoiceService invoiceService;
	@Autowired
	private IntoTicketService intoTicketService;
	@Autowired
	private GoTicketAndIntoTicketService goTicketAndIntoTicketService;
//	@Autowired
//	private FeeCheckService feeCheckService;

//	/**
//	 * 出票管理-列表
//	 * @param page
//	 * @throws Exception
//	 */
//	@RequestMapping(value="/index")
//	public ModelAndView list(Page page, HttpServletRequest req) throws Exception{
//		Map param = getParams(req);//new PageData(req);
//		TFinFeeCheckExample e = new TFinFeeCheckExample();
//		e.createCriteria();
//		e.setPage(page);
//		handlePageParams(param,e.getPage());
//		ModelAndView mv = getModelAndView();
//		try{
//			List<TFinFeeCheck> ret = feeCheckService.selectByPage(e,param,page);
//			//PageData pd = getPageData();
//			mv.addObject("dataList",ret);
//			mv.addObject("pd", page);
//			mv.addObject("QX",Jurisdiction.getHC());//按钮权限
//			mv.setViewName(PREFIX.concat("invoice_index_create"));
//		} catch(Exception ex){
//			log.error("query-check-error:"+param,ex);
//		}
//		return mv;
//	}
//	/**
//	 * 出票管理-列表
//	 * @param page
//	 * @throws Exception
//	 */
//	@RequestMapping(value="/indexIncome")
//	public ModelAndView listIncome(Page page, HttpServletRequest req) throws Exception{
//		Map param = getParams(req);//new PageData(req);
//		TFinFeeCheckExample e = new TFinFeeCheckExample();
//		e.createCriteria();
//		e.setPage(page);
//		handlePageParams(param,e.getPage());
//		ModelAndView mv = getModelAndView();
//		try{
//			List<TFinFeeCheck> ret = feeCheckService.selectByPage(e,param,page);
//			//PageData pd = getPageData();
//			mv.addObject("dataList",ret);
//			mv.addObject("pd", page);
//			mv.addObject("QX",Jurisdiction.getHC());//按钮权限
//			mv.setViewName(PREFIX.concat("invoice_index_income"));
//		} catch(Exception ex){
//			log.error("query-check-error:"+param,ex);
//		}
//		return mv;
//	}
	/**
	 * 列表查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView listInvoice()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName(PREFIX.concat("invoiceManage/invoice_list"));
		return view;
		
	}
	/**
	 * 开票待处理信息
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/invoiceInfo")
	@ResponseBody
	public ResponseResult invoiceInfo(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = invoiceService.invoiceInfo(pd);
		return ResponseResult.success(new PageInfo(list));
		
	}
	/**
	 * 开票待处理信息
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/invoiceDetil")
	@ResponseBody
	
	public ResponseResult invoiceDetil(int page)throws Exception{
//		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = invoiceService.invoiceDetil(pd);
		return ResponseResult.success(new PageInfo(list));
		
	}
	/**
	 * 保存开票信息（开具）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveInvoiceInfo")
	@ResponseBody
	public String saveInvoiceInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		Date d = new Date();   
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String dateNowStr = sdf.format(d);  
		pd.put("invoice_date", dateNowStr);
		String USERNAME = Jurisdiction.getUsername();//当前登录的用户名        	
		pd.put("invoice_person", USERNAME);
		invoiceService.saveInvoiceInfo(pd);
		PageData pdq = new PageData();
		pdq.put("is_issue", "1");
		pdq.put("invoice_app_code", pd.getString("invoice_app_code"));
		invoiceService.updateApplyInfo(pdq);
		return null;
	}
	/**
	 * 作废和冲红
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateInvoiceState")
	@ResponseBody
	public String updateInvoiceState()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		invoiceService.isCancelAndRed(pd);
		return null;
		
	}
	/**
	 * 否决
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateState")
	@ResponseBody
	public String updateState()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		invoiceService.updateApplyInfo(pd);
		return null;
		
	}
	/**
	 * 进票管理页面跳转
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/intoTicket")
	public ModelAndView intoTicket()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName(PREFIX.concat("invoiceManage/into_ticket_list"));
		return view;
		
	}
	/**
	 * 进票管理数据
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoTicketInfo")
	@ResponseBody
	public ResponseResult intoTicketInfo(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = intoTicketService.findIntoTicketInfo(pd);
		return ResponseResult.success(new PageInfo(list));	
	}
	/**
	 * 发票信息
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findinvoiceInfo")
	@ResponseBody
	public ResponseResult findinvoiceInfo(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = intoTicketService.findInvoiceInfo(pd);
		return ResponseResult.success(new PageInfo(list));	
	}
	/**
	 * 发票名目
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findinvoiceItem")
	@ResponseBody
	public ResponseResult findinvoiceItem(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = intoTicketService.findInvoiceItem(pd);
		return ResponseResult.success(new PageInfo(list));	
	}
	/**
	 * 费用项目
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findFeeProject")
	@ResponseBody
	public ResponseResult findFeeProject(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = intoTicketService.findFeeProject(pd);
		return ResponseResult.success(new PageInfo(list));	
	}
	/**
	 * 更新发票信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateInvoiceInfo")
	@ResponseBody
	public String updateInvoiceInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("now_state").equals("1")){
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			pd.put("check_date", df.format(new Date()));
		}
		intoTicketService.updateInvoice(pd);
		return null;
		
	}
	/**
	 * 出票查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goTicketPage")
	@ResponseBody
	public ModelAndView goTicketPage()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName(PREFIX.concat("invoiceManage/go_ticket"));
		return view;
		
	}
	/**
	 * 出票数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/goTicketInfo")
	@ResponseBody
	public ResponseResult goTicketInfo(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = TranscodUntil.Transcod(pd);
		List<Map> list = goTicketAndIntoTicketService.findGoTicketInfo(pd);
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 出票数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoTicketData")
	@ResponseBody
	public ResponseResult intoTicketData(int page)throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = TranscodUntil.Transcod(pd);
		List<Map> list = goTicketAndIntoTicketService.findIntoTicketInfo(pd);
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 发票抬头和发票号
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findInvoice")
	@ResponseBody
	public ResponseResult findInvoice()throws Exception{
		List<Map> list = goTicketAndIntoTicketService.findInvoice();
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 发票类别
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findFeeType")
	@ResponseBody
	public ResponseResult findFeeType()throws Exception{
		List<Map> list = goTicketAndIntoTicketService.findFeeType();
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 托单编号
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findBillCode")
	@ResponseBody
	public ResponseResult findBillCode()throws Exception{
		List<Map> list = goTicketAndIntoTicketService.findBillCode();
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 申请号
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findInvoiceAppCode")
	@ResponseBody
	public ResponseResult findInvoiceAppCode()throws Exception{
		List<Map> list = goTicketAndIntoTicketService.findInvoiceAppCode();
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 发票抬头和发票号(成本)
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findInvoiceTwo")
	@ResponseBody
	public ResponseResult findInvoiceTwo()throws Exception{
		List<Map> list = goTicketAndIntoTicketService.findInvoiceTwo();
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 发票类别（成本）
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findFeeTypeTwo")
	@ResponseBody
	public ResponseResult findFeeTypeTwo()throws Exception{
		List<Map> list = goTicketAndIntoTicketService.findFeeTypeTwo();
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 申请号
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findInvoiceAppCodeTwo")
	@ResponseBody
	public ResponseResult findInvoiceAppCodeTwo()throws Exception{
		List<Map> list = goTicketAndIntoTicketService.findInvoiceAppCodeTwo();
		return ResponseResult.success(new PageInfo(list));	
		
	}
	/**
	 * 分类统计
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/classifiedStatistic")
	@ResponseBody
	public ModelAndView index()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName(PREFIX.concat("invoiceManage/classifiedStatistic/classified_index_list"));
		return view;
		
	}
	
}
